package com.tanhua.controller;

import com.tanhua.autoconfig.template.JWTTemplate;
import com.tanhua.server.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/user")
@Slf4j
public class LoginController {

    @Autowired
    private UserService userService;
    @Autowired
    private JWTTemplate jwtTemplate;
//    @Autowired
//    private UserApi userApi;

    /**
     * 获取登录验证码
     * @param map 参数封装
     * @return void
     */
    @PostMapping("/login")
    public ResponseEntity<Void> login(@RequestBody Map map) {
        Object phone = map.get("phone");
        userService.sendMsg(phone.toString());
        return ResponseEntity.ok(null);
    }

    @PostMapping("/loginVerification")
    public ResponseEntity<Map> loginVerification(@RequestBody Map map) {
        String phone = map.get("phone").toString();
        String code = map.get("verificationCode").toString();
        Map retMap = userService.loginVerification(phone, code);
        if (retMap == null) {
            return ResponseEntity.status(401).body(null);
        }
        return ResponseEntity.ok(retMap);
    }
}
